package ai.controller;

import ai.service.InMemoryVectorStore;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/ai")
public class AiDemoController {

    @Autowired(required = false)
    private ChatClient chatClient;

    @Autowired
    private InMemoryVectorStore vectorStore;

    @PostMapping("/ask")
    public Map<String, Object> ask(@RequestBody Map<String, Object> body) {
        String prompt = String.valueOf(body.getOrDefault("prompt", ""));
        String docs = vectorStore.retrieve(prompt, 2000);
        String fullPrompt = (docs.isBlank() ? "" : "[参考资料]\n" + docs + "\n\n") + "user: " + prompt;
        String reply = chatClient == null ? "ChatClient not configured" : chatClient.prompt().user(fullPrompt).call().content();

        Map<String, Object> res = new HashMap<>();
        res.put("reply", reply);
        res.put("timestamp", System.currentTimeMillis());
        return res;
    }
}


